/**
 * 
 */
package net.thelostlands.thelostmod.listeners;

import net.thelostlands.thelostmod.PermHandler;
import net.thelostlands.thelostmod.Permissions;
import net.thelostlands.thelostmod.LostMod;
import net.thelostlands.thelostmod.Permissions.Perms;

import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityCombustEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityListener;
import org.bukkit.event.entity.EntityTargetEvent;

/**
 * @author sleaker, faaabio
 *
 */
public class LMEntityListener extends EntityListener {
        
	private LostMod plugin;
	
    public LMEntityListener(LostMod plugin) {
    	this.plugin = plugin;
    }
    
    public void onEntityTarget (EntityTargetEvent event) {
        if (event.isCancelled() && !(event.getTarget() instanceof Player))
            return;
        
        if (event.getTarget() instanceof Player ) {
            Player player = ((Player) event.getTarget());
            if( (PermHandler.isGod(player.getName()) || PermHandler.isInvisible(player.getName())) && Permissions.has(player, Perms.NO_AGGRO))
                event.setCancelled(true);
        }
    }
    
    public void onEntityDamage (EntityDamageEvent event) {
        if (event.isCancelled())
            return;
        
        if (event.getEntity() instanceof Player) {
            String playerName = ((Player) event.getEntity()).getName();
            if (PermHandler.isGod(playerName))
                event.setCancelled(true);
        }
    }
    
    public void onEntityCombust (EntityCombustEvent event) {
        if (event.isCancelled())
            return;
        
        if (event.getEntity() instanceof Player) {
            String playerName = ((Player) event.getEntity()).getName();
            if (PermHandler.isGod(playerName)) {
                event.setCancelled(true);
                plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new douse((Player) event.getEntity()), 2);
            }
        }
    }
    
    public class douse implements Runnable {
    	
    	Player player;
    	douse(Player player) {
    		this.player = player;
    	}
    	
		public void run() {
			player.setFireTicks(0);
		}
    }
}
